// pages/getDoctors/register/register.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        urlArg: null,
        message: null,
        doctor_id: null,
        raw_message: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var urlArg_doctor_id = 'urlArg.doctor_id';
        var that = this;
        wx.request({
          url: app.globalData.base_request + "/doctor/getScheduleByDoctorId/" + options.doctor_id,
          method: 'GET',
          success(res) {
            console.log('success');
            console.log(res.data);
            that.setData({
                raw_start_time: res.data.start_time,
                message: res.data,
                doctor_id: options.doctor_id
              });
            for(var key in that.data.message)
            {
                var str = that.data.message[key].start_time;
                var strArray=str.split(".");
                strArray = strArray[0].split("T")
                var strDate=strArray[0].split("-");
                var strTime=strArray[1].split(":");
                var date=new Date(strDate[0],(strDate[1]-parseInt(1)),strDate[2],strTime[0],strTime[1],strTime[2]) 
                date.setHours(date.getHours());
                var start_time = 'message' + '[' + key + ']' + '.start_time';
                var raw_message_ = 'raw_message' + '[' + key + ']' + '.start_time';
                // console.log(that.data.message[key].start_time.split('.')[0].replaceAll('-',',').replaceAll('T',',').replaceAll(':',','));
                // var date = new Date(that.data.message[key].start_time.split('.')[0].replaceAll('-',',').replaceAll('T',',').replaceAll(':',','));
                var start_time_str = date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日";
                start_time_str += date.getHours() + '点';
                that.setData({
                    [raw_message_]: date,
                    [start_time]: start_time_str
                });
            }

          },
          fail: function() {
            console.log("failure");  
            wx.showToast({  
                title: '无法连接服务器',  
                icon: 'loading',  
                duration: 2000  
            })  
          }
        });
        this.setData({
            [urlArg_doctor_id]: options.doctor_id,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    formSubmit: function(event){
      var time = require('../../../../utils/util.js');
      var start_time_ = this.data.raw_message[event.detail.value.time].start_time;
      var start_time = time.formatTime(start_time_);
      var that = this;
      wx.request({
          url: app.globalData.base_request + "/patient/register/" + app.globalData.userId + "/" + that.data.doctor_id + "/" + start_time,
          method: 'GET',
          success(res) {
            console.log('success');
            console.log(res.data);
            wx.navigateBack({
              delta: 0,
            })
          },
          fail: function() {
            console.log("failure");  
            wx.showToast({  
                title: '无法连接服务器',  
                icon: 'loading',  
                duration: 2000  
            })  
          }
      });
    }
})